<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="storageManager_id" label="库管员账号">
				<uni-data-select v-model="formData.storageManager_id" :localdata="kuGuanRange"
					@change="chooseKuGuan"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="storageName" label="仓库名称" required>
				<uni-easyinput placeholder="仓库名称" v-model="formData.storageName" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="province" label="省" required>
				<uni-easyinput placeholder="省" v-model="formData.province" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="city" label="市" required>
				<uni-easyinput placeholder="市" v-model="formData.city" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="area" label="区" required>
				<uni-easyinput placeholder="区" v-model="formData.area" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="address" label="详细地址" required>
				<uni-easyinput placeholder="详细地址" v-model="formData.address" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="state" label="启用状态">
				<switch @change="binddata('state', $event.detail.value)" :checked="formData.state"></switch>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/tab_StorageInfo.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'tab_StorageInfo';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"storageManager_id": "",
				"storageName": "",
				"province": "",
				"city": "",
				"area": "",
				"address": "",
				"state": true
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				},
				kuGuanRange: []
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
			this.getkuGuanRange()
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			async getkuGuanRange() {
				await db.collection('uni-id-users').field("username,nickname").get().then((res) => {
					// console.log(res.result.data)
					const receivedData = res.result.data
					this.kuGuanRange = receivedData.map(item => ({
						value: item._id,
						text: `账号: ${item.username} 昵称: ${item.nickname}`
					}));
				}).catch((err) => {
					uni.showModal({
						content: err.message || '获取库管员账号失败',
						showCancel: false
					})
				})
			},
			//点击选择仓库管理员
			chooseKuGuan(e) {
				console.log(e)
			}
		}
	}
</script>